package com.company.test30;

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.io.*;
import java.net.*;

/**
 * Created by v on 16-11-30.
 */
public class  Test30_01 extends JFrame{
    private JTextArea jta=new JTextArea();

    public Test30_01(){
        setLayout(new BorderLayout());
        add(new JScrollPane(jta),BorderLayout.CENTER);

        setTitle("Server");
        setSize(500,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        try{
            ServerSocket serverSocket=new ServerSocket(8000);
            jta.append("Server started at "+new Date()+'\n');

            Socket socket=serverSocket.accept();

            DataInputStream inputFromClient=new DataInputStream(
                    socket.getInputStream()
            );

            DataOutputStream outputToClient=new DataOutputStream(
                    socket.getOutputStream()
            );

            while (true){
                double radius=inputFromClient.readDouble();

                double area=radius*radius*Math.PI;

                outputToClient.writeDouble(area);

                jta.append("Radius received from client: "+ radius+'\n');
                jta.append("Area found: "+area+'\n');
            }
        }catch (IOException ex){
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Test30_01();
    }
}
